#!/usr/bin/perl

# http://poe.perl.org/?POE_Cookbook/Gtk_Interfaces
#
# This sample program creates a very simple Gtk counter.  Its
# interface consists of three widgets: A label, a rapidly increasing
# counter, and a button to reset that counter.

use warnings;
use strict;

# Gtk support is enabled if the Gtk module is used before POE itself.
# POE::Loop::Glib doesn't initialize Gtk2 (for obvious reasons), so we
# do it here

use Gtk2-init;
use Gtk2::SimpleList;
use POE qw(Component::IRC::State Component::IRC::Plugin::Connector);
use POE::Component::IRC::Common qw(parse_user strip_color strip_formatting);
use POE::Kernel {loop => "Glib"};
use POE::Session;
use Core::ConfigFile;
use Core::Format;

use utf8;

use Data::Dumper;

my $server = "irc.freenode.net";
my $port = 6667;
my $nickname = "bot_tester";
my $ircname = "Testing of bot";
my $channel = "#rushtest";

# Create the session that will drive the user interface.

my $irc = POE::Component::IRC::State->spawn( 
     nick => $nickname,
     server => $server,
     port => $port,
     ircname => $ircname,
 ) or die "Oh noooo! $!";

POE::Session->create(
	package_states => [
		main => [ qw(_default _start irc_001 irc_public irc_notice irc_chan_sync irc_nick_sync irc_join irc_msg) ],
	],
  inline_states => {
  }
);

# Run the program until it is exited.

$poe_kernel->run();
exit 0;

my $messages;
my $buffer;
my $input;
my $nicks;
my $window;

sub push_buffer {
	my ($start, $end) = $buffer->get_bounds();
	my $text = strip_color(strip_formatting($_[0]));
	shift;
	$buffer->insert_with_tags_by_name($end, $text, @_);
}

sub irc_msg {
	my ($user, $recipients, $text) = @_[ARG0..ARG2];
	my $nick = parse_user($user);

	push_buffer "PRIV <$nick> $text\n", "fg_red";
}

sub irc_join
{
	my ($user, $channel) = (@_[ARG0..ARG1]);
	my ($nick, $username, $host) = parse_user($user);

	push_buffer "$nick ($host) dołączył do $channel\n", "fg_pink";
}

sub irc_chan_sync
{
	@{$nicks->{data}} = map { [$_] } $irc->channel_list($channel);
	push_buffer "Zsynchronizowano do kanału $channel!\n";
}

sub irc_nick_sync
{
	@{$nicks->{data}} = map { [$_] } $irc->channel_list($channel);
}

sub irc_001 {
	push_buffer "Połączono do serwera IRC!\n";
	$irc->yield(join => $channel);
}

sub irc_notice {
	my ($user, $recipients, $text) = @_[ARG0..ARG2];
	my $nick = parse_user($user);
	push_buffer "$nick : $text\n", "fg_orange";
}

sub irc_public {
	my ($user, $where, $what) = @_[ARG0 .. ARG2];
	my $nick = parse_user($user);
	push_buffer "<$nick> $what\n";
}

sub _default {

}

sub _start {
	ui_start(@_);
	irc_start(@_);
}


sub about_box
{

  my $dialog = Gtk2::MessageDialog->new ($window,
		'destroy-with-parent',
		'info',
		'ok',
		"Prosty klient IRC - część dystrybucji bota nimfomanka

Wersja:  0.1
Autor : Damian Kaczmarek

Program służy do testowania bota nimfomanka, implementuje podstawowe funkcje
protokołu IRC.");

	$dialog->signal_connect (response => sub {
             my ($self, $response) = @_;
             $self->destroy;
  	});
   $dialog->show();
}

sub ui_start {
  my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];

  my $window = Gtk2::Window->new("toplevel");
  $heap->{main_window} = $window;
  $kernel->signal_ui_destroy($heap->{main_window});

  $heap->{main_window}->set_size_request(640, 480);

  my $box = Gtk2::VBox->new(0, 0);



  my $menu_file = Gtk2::Menu->new();
  my $menu_quit = Gtk2::MenuItem->new('_Wyjście');
  my $menu_refresh = Gtk2::MenuItem->new('_Odśwież');

  $menu_quit->signal_connect('activate' => sub { $window->destroy; });

  $menu_file->append($menu_quit);

  my $menu_help = Gtk2::Menu->new();
  my $menu_about = Gtk2::MenuItem->new('_O programie');
  $menu_about->signal_connect('activate' => \&about_box);

  $menu_help->append($menu_about);

  my $menu_item_file = Gtk2::MenuItem->new('_Program');
  my $menu_item_help = Gtk2::MenuItem->new('P_omoc');
  $menu_item_file->set_submenu($menu_file);
  $menu_item_help->set_submenu($menu_help);

  my $menu_bar = Gtk2::MenuBar->new;

  $menu_bar->append($menu_item_file);
  $menu_bar->append($menu_item_help);
  $box->pack_start($menu_bar, 0, 0, 0);


  $heap->{main_window}->add($box);

  my $hbox = Gtk2::HBox->new(0, 0);
  $box->pack_start($hbox, 1, 1, 0);


  $nicks = Gtk2::SimpleList->new (
                ''    => 'text',
              );
#  $nicks->signal_connect(row_activated => sub { print "DUPA\n"; });
  $nicks->set_headers_visible(0);
	$nicks->set_size_request(120, -1);

#   push @{$nicks->{data}}, [ 'nimfo' ];
#   push @{$nicks->{data}}, [ 'rushpl' ];
#   push @{$nicks->{data}}, [ "blah" ];
#  $nicks->set_editable(0);
#  my $nicks = Gtk2::TreeView->new();
#  $nicks->set_editable(0);
	$messages = Gtk2::TextView->new;
	$messages->set_editable(0);
#	$messages->set_right_margin(600);
	$messages->set_size_request(600, -1);

  $hbox->pack_start($messages, 1, 1, 0);
  $hbox->pack_start(Gtk2::VSeparator->new, 0, 1, 4);
  $hbox->pack_start($nicks, 1, 1, 0);

#  $hbox->pack_start($nicks, 1, 1, 0);
  $messages->set_cursor_visible(0);
  $buffer = Gtk2::TextBuffer->new;

  my $blue = $buffer->create_tag("fg_blue", foreground => "blue");
  my $yellow = $buffer->create_tag("fg_yellow", foreground => "yellow");
  my $orange = $buffer->create_tag("fg_orange", foreground => "orange");
  my $pink = $buffer->create_tag("fg_pink", foreground => "pink");
  my $red = $buffer->create_tag("fg_red", foreground => "red");

  $messages->set_buffer($buffer);

  my $label = Gtk2::Label->new("Counter");

  $heap->{counter}       = 0;
  $heap->{counter_label} = Gtk2::Label->new($heap->{counter});


  $input = Gtk2::Entry->new;
  $box->pack_start($input, 0, 0, 4);

  $heap->{main_window}->show_all();
  $input->grab_focus();


  $input->signal_connect('activate' => sub {
		my ($self, $response) = @_;
		my $msg = $self->get_text();
		if($msg =~ /^\/msg (\w+) (.*?)$/) {			
			$irc->yield(privmsg => $1, $2);
			push_buffer "-> $1 -> $2\n", "fg_red";
		}
		else {
			$irc->yield(privmsg => $channel, $msg);
			push_buffer "<$nickname> $msg\n";
		}
		$self->set_text("");

	});

}

sub irc_start {
	my ($kernel, $heap) = @_[KERNEL ,HEAP];
	$irc->yield( register => 'all' );

	$heap->{connector} = POE::Component::IRC::Plugin::Connector->new();

	$irc->plugin_add( 'Connector' => $heap->{connector} );

	$irc->yield ( connect => { Nick => $nickname, Server => $server } );

	$kernel->delay( 'lag_o_meter' => 60 );
	return;
}

